#include <stdlib.h>
#include <stdio.h>

#include "sock.h"

int main (int argc, char * argv[])
{
	cconf_sock_t * s;
	if (argc < 3)
	{
		fprintf(stderr,"config_set <path> [<value>]\n");
		exit(1);
	}
	
	char *path = argv[1];
	char *name = argv[2];
	
	char *value = argc > 2 ? argv[3] : NULL;
	char *in = NULL;
	char *out = NULL;	
	
	s = cconf_sock_create(path, CCONF_SOCK_CLIENT);
	
	if (!s)
	{
		fprintf(stderr,"failed to create socket at %s\n", path);
		exit(1);
	}
	
	if (value)
		asprintf(&in, "%s %s", name, value);
	else
		asprintf(&in, "%s", name);
	
	cconf_sock_send_string(s, in);
			
	out = cconf_sock_get_string(s);	
	
	if (out == NULL)
	{
		fprintf(stderr,"no running configuration service available with name %s\n", path);
		exit(1);
	}
	
	fprintf(stderr, "reply: %s\n", out);
		
	cconf_sock_destroy(s);

	free(out);
	free(in);
	
	exit(0);
}
